# 调用栈
# 如果错误没有被捕获，它就会一直往上抛，最后被python解释器捕获，打印一个错误信息，然后程序退出
# def foo(s):
#     return 10 / int(s)
#
# def bar(s):
#     return foo(s) * 2
#
# def main():
#     bar('0')
#
# main()

'''
错误信息：
Traceback (most recent call last):
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 12, in <module>
    main()
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 10, in main
    bar('0')
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 7, in bar
    return foo(s) * 2
           ^^^^^^
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 4, in foo
    return 10 / int(s)
           ~~~^~~~~~~~
ZeroDivisionError: division by zero
'''


# 记录错误
'''
如果不捕获错误，自然可以让python解释器来打印出错误堆栈，但程序也被结束了。既然我们能捕获错误，就可以把错误堆栈打印出来，然后分析错误原因，同时，让
程序继续下去
'''
# python内置的 logging 模块可以非常容易地记录错误信息：
import logging

def foo(s):
    return 10 / int(s)

def bar(s):
    return foo(s) * 2

def main():
    try:
        bar('0')
    except Exception as e:
        logging.exception(e)

main()
print('END')

# 同样是出错，但程序打印完错误信息后会继续执行，并正常退出：
'''
ERROR:root:division by zero
Traceback (most recent call last):
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 47, in main
    bar('0')
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 43, in bar
    return foo(s) * 2
           ^^^^^^
  File "G:\www_data\python_project\python-demo\liaoxuefeng\09-错误处理\demo2.py", line 40, in foo
    return 10 / int(s)
           ~~~^~~~~~~~
ZeroDivisionError: division by zero
END
'''

